package com.isatechindonesia.isatracker;

import com.isatechindonesia.isatracker.service.GPSLoggerService;
import com.isatechindonesia.isatracker.service.GPSSendData;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

	private Button btnStart, btnStop;
	private TextView txtId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final Intent myIntent = new Intent(MainActivity.this,
				GPSLoggerService.class);
		
		final Intent isIntent = new Intent(MainActivity.this,
				GPSSendData.class);


		btnStart = (Button) findViewById(R.id.btn_start);
		btnStop = (Button) findViewById(R.id.btn_stop);
		txtId = (TextView) findViewById(R.id.textId);
		
		SharedPreferences pref = getApplicationContext().getSharedPreferences("gpsconfig", MODE_PRIVATE);
		String idgps = pref.getString("idgps", "0000");				
		txtId.setText("Tracker ID " + idgps);

		if (isMyServiceRunning()) {
			btnStart.setEnabled(false);
			btnStop.setEnabled(true);
		} else {
			if (!idgps.equals("0000") && !idgps.isEmpty())
				btnStart.setEnabled(true);
			else
				btnStart.setEnabled(false);

			btnStop.setEnabled(false);
		}

		btnStart.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startService(myIntent);
				startService(isIntent);

				btnStart.setEnabled(false);
				btnStop.setEnabled(true);
			}
		});

		btnStop.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				stopService(myIntent);
				stopService(isIntent);
				
				btnStart.setEnabled(true);
				btnStop.setEnabled(false);
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			Intent i = new Intent(MainActivity.this, SettingActivity.class);			
			finish();
			startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}	
	
	private boolean isMyServiceRunning() {
		ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager
				.getRunningServices(Integer.MAX_VALUE)) {
			if ("com.isatechindonesia.isatracker.service.GPSLoggerService"
					.equals(service.service.getClassName()) || 
				"com.isatechindonesia.isatracker.service.GPSSendData"
					.equals(service.service.getClassName())) {
				
				return true;
			}
		}
		return false;
	}

}
